package com.inlycat.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inlycat.entity.Store;
import com.inlycat.service.StoreService;

/**
 * @author jarvan4dev@163.com
 * @since 2015年8月24日下午3:18:05
 * @category
 **/
@ControllerAdvice
@RequestMapping("/store")
public class StoreController {

	@Resource
	private StoreService storeService;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(Store store) throws BindException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = storeService.insert(store);
		if (result) {
			resultMap.put("code", 1);
			resultMap.put("message", "success");
		} else {
			resultMap.put("code", 0);
			resultMap.put("message", "fail");
		}
		return resultMap;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(Store store) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = storeService.delete(store);
		if (result) {
			resultMap.put("code", 1);
			resultMap.put("message", "success");
		} else {
			resultMap.put("code", 0);
			resultMap.put("message", "fail");
		}
		return resultMap;
	}

	@RequestMapping(value = "/select", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> select(Long userId, String pageNum) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Store> stores = storeService.selectByUserId(userId, pageNum);
		if (stores == null) {
			resultMap.put("code", 0);
			resultMap.put("message", "暂无收藏");
		} else {
			resultMap.put("code", 1);
			resultMap.put("message", "success");
			resultMap.put("data", stores);
		}
		return resultMap;
	}

}
